package com.vains.controller;

import com.vains.service.IOauthPermissionService;
import com.vains.system.model.Result;
import com.vains.system.util.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author vains
 * @since 2020-07-03
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api(tags = "权限相关接口")
@RequestMapping("/oauthPermission")
public class OauthPermissionController {

    private final IOauthPermissionService oauthPermissionService;

    @GetMapping("/hasAuth")
    @ApiOperation(value = "校验该请求是否可以访问参数 url", notes = "参数二可不填,但是要在请求头中添加, 一般由网关内部调用")
    public Result<Boolean> hasAuth(@RequestParam("url") String url, @RequestParam(value = "access_token", required = false) String access_token) {
        log.info("接收到token: {}", access_token);
        // 第二个参数是为了可以让feign将令牌带过来
        boolean hasPermission = oauthPermissionService.hasPermission(url);
        return ResultUtils.success(hasPermission);
    }

}
